#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

struct TreeNode
{
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
};

int DepthBig(struct TreeNode* root)
{
    if (root == NULL)
    {
        return 0;
    }

    int left = DepthBig(root->left);
    int right = DepthBig(root->right);


    return left > right ? left + 1 : right + 1;
}

int DepthSmall(struct TreeNode* root)
{
    if (root == NULL)
    {
        return 0;
    }

    int left = DepthSmall(root->left);
    int right = DepthSmall(root->right);


    return left > right ? left + 1 : right + 1;
}

bool isBalanced(struct TreeNode* root) {
    if (root == NULL)
    {
        return true;
    }
    struct TreeNode* cur = root;


    int big = DepthBig(cur->left);
    cur = root;
    int small = DepthSmall(cur->right);

    if (big < small)
    {
        int tmp = big;
        big = small;
        small = tmp;
    }

    int sub = big - small;

    if (sub > 1)
    {
        return false;
    }

    return isBalanced(root->left) && isBalanced(root->right);

}

struct TreeNode* CreateNode(int x)
{
    struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    if (node == NULL)
    {
        return;
    }
    node->val = x;
    node->left = node->right = NULL;
}

int main()
{
    struct TreeNode* node1 = CreateNode(1);
    struct TreeNode* node2 = CreateNode(2);
    struct TreeNode* node3 = CreateNode(3);
    /*struct TreeNode* node4 = CreateNode(4);
    struct TreeNode* node5 = CreateNode(5);
    struct TreeNode* node6 = CreateNode(6);
    struct TreeNode* node7 = CreateNode(8);*/

    node1->right = node2;
    node2->right = node3;

   /* node1->left = node2;
    node1->right = node3;
    node2->left = node4;
    node2->right = node5;
    node3->left = node6;
    node4->left = node7;*/

    printf("%d", isBalanced(node1));

}